import * as os from 'os';
import * as fs from 'fs';
import * as path from 'path';

/**
 * 获取数据库用户名和密码（本地用户目录文件形式）
 * @returns { username, password }
 */
export const getMySqlUserInfo = () => {
  const homedir = os.homedir();
  const usernamePath = path.resolve(homedir, '.vben', 'username');
  const passwordPath = path.resolve(homedir, '.vben', 'password');
  const username = fs.readFileSync(usernamePath).toString().trim();
  const password = fs.readFileSync(passwordPath).toString().trim();

  return {
    username,
    password
  };
};

export const success = (result, message) => {
  return {
    code: 0,
    result,
    message
  };
};

export const error = (message) => {
  return {
    code: -1,
    message
  };
};

export const wrapperResponse = (promise, message) => {
  return promise
    .then((result) => {
      return success(result, message);
    })
    .catch((err) => {
      return error(err.message);
    });
};
